package com.ternnetwork.batch.item.writer.sms;

import java.util.ArrayList;
import java.util.List;

import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

import com.ternnetwork.batch.item.mapper.sms.ModelToMobilePhoneShortMessageMapper;
import com.ternnetwork.batch.model.demo.CreditBill;
import com.ternnetwork.batch.sms.MobilePhoneShortMessage;
import com.ternnetwork.batch.sms.MobilePhoneShortMessageSender;



public  class MobilePhoneShortMessageItemWirter<T> implements ItemWriter<T>, InitializingBean{

	
	private MobilePhoneShortMessageSender sender;
	
	private ModelToMobilePhoneShortMessageMapper<T> messageMapper;

	
	public void afterPropertiesSet() throws Exception {
		Assert.notNull(sender, "The sender must be set");
		Assert.notNull(messageMapper, "The messageMapper must be set");
	}

	public void write(List<? extends T> items) throws Exception {
	
		List<MobilePhoneShortMessage> massageList=new ArrayList<MobilePhoneShortMessage>();
		for (Object object : items) {//重新组装发送消息对象
           MobilePhoneShortMessage sms=(MobilePhoneShortMessage) messageMapper.map(object);
       	   massageList.add(sms);
   		}
		sender.send((MobilePhoneShortMessage[])massageList.toArray(new MobilePhoneShortMessage[massageList.size()]));
		
	}

	public MobilePhoneShortMessageSender getSender() {
		return sender;
	}

	public void setSender(MobilePhoneShortMessageSender sender) {
		this.sender = sender;
	}

	public ModelToMobilePhoneShortMessageMapper<T> getMessageMapper() {
		return messageMapper;
	}

	public void setMessageMapper(ModelToMobilePhoneShortMessageMapper<T> messageMapper) {
		this.messageMapper = messageMapper;
	}
	

}
